<?php

namespace App\Console\Commands;

use App\Jobs\AssociateShowWithUnsortedEpisode;
use Illuminate\Console\Command;
use DB;

class SortEpisode extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sort:episode';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '整理没有归档的美剧,创建任务队列';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $show_name_list = DB::table('shows')->select(DB::raw('LENGTH(`name_en`) as `name_length`, `name_en`,`id`'))->orderBy('name_length', 'desc')->get();
        foreach ($show_name_list as $showStdObj) {
            if ($showStdObj->name_length > 3) {
                $job = new AssociateShowWithUnsortedEpisode($showStdObj);
                dispatch($job);
            }
        }


    }
}
